﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WilsonWebsite
{
    public partial class ProList : System.Web.UI.Page
    {
        private int serId = 0;
        protected string serName = string.Empty;
        protected string catName = string.Empty;
        private string strCon = Global.strCon;
        protected string imageWeb = Global.imageWeb;
        private int proNum = Global.proNum;
        protected Dictionary<string, string> dic = new Dictionary<string, string>();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                if (Request["serId"] != null && Request["serId"] != "")
                {
                    serId = Convert.ToInt32(Request["serId"]);
                    BindProd(strCon, serId);
                    SerInfo(Global.strCon, serId);
                }
                else 
                {
                    BindAllProd(Global.strCon);
                    dic["Title"] = "Wilson - 所有产品列表";
                    dic["Keywords"] = "所有产品列表";
                    dic["Discription"] = "所有产品列表";
                }
            }
        }
        //该系列下的产品
        protected void BindProd(string strCon, int serId) 
        {
            using (DataTable dtp = Bll.Product.MULTIPLE(strCon,serId)) 
            {
                if (dtp != null && dtp.Rows.Count > 0)
                {
                    serName = "<a href=\"Cat-" + dtp.Rows[0]["CatId"].ToString() + ".html\" title=\"" + dtp.Rows[0]["SeriesName"].ToString() + "\">" + dtp.Rows[0]["SeriesName"].ToString() + "</a>";
                    dtp.Columns.Add("RowId", typeof(Int32)).SetOrdinal(1);
                    for (int i = 0; i < dtp.Rows.Count; i++) 
                    {
                        dtp.Rows[i]["RowId"] = i + 1;
                    }
                    var query = from q in dtp.AsEnumerable()
                                where Convert.ToInt32(q["RowId"]) <= proNum
                                select q;
                    this.repProd.DataSource = query.CopyToDataTable<DataRow>().DefaultView;
                    this.repProd.DataBind();
                }
            }
        }

        //所以产品
        protected void BindAllProd(string strCon)
        {
            using (DataTable dt = Bll.Product.MULTIPLE(strCon))
            {
                if (dt != null && dt.Rows.Count != 0)
                {
                    dt.Columns.Add("RowId", typeof(Int32)).SetOrdinal(1);
                    for (int i = 0; i < dt.Rows.Count; i++) 
                    {
                        this.repProd.DataSource = dt.DefaultView;
                        this.repProd.DataBind();
                    }
                }
            }
        }

        //系列信息
        protected void SerInfo(string strCon,int serId) 
        {
            using (DataTable dtSer = Bll.Series.Single(strCon, serId)) 
            {
                if (dtSer != null && dtSer.Rows.Count > 0)
                {
                    dic["Title"] = dtSer.Rows[0]["SeriesName"].ToString();
                    dic["Keywords"] = dtSer.Rows[0]["SeriesName"].ToString();
                    dic["Discription"] = dtSer.Rows[0]["Description"].ToString();
                }
            }
        }
    }
}
